package com.lcsw.background.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lcsw.background.domain.AdminPlus;
import com.lcsw.background.domain.Tradeserial;
import com.lcsw.background.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * 管理员
 */
@RestController
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    AdminService adminService;
    @Autowired
    HttpServletRequest request;

    /**
     * 管理员条件查询
     *
     * @param
     * @return
     */
    @RequestMapping("/queryList")
    @ResponseBody
    public JSONObject queryList(@RequestBody(required = false) String json1) {
        JSONObject object = new JSONObject();
        if (json1 == null || json1 == "") {//没有参数时,返回查到的所有用户
            List<AdminPlus> list = adminService.payList("");
            object.put("list", list);
            return object;
        }
        int pageNum = 0;
        int pageSize = 0;
        String username = "";
        String condition = "";
        JSONObject json = null;
        try {
            json = (JSONObject) JSONObject.parse(json1);
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (StringUtils.isEmpty(json.get("pageNum"))) {
            pageNum = 1;
        } else {
            pageNum = Integer.parseInt(json.get("pageNum").toString());
        }

        if (StringUtils.isEmpty(json.get("pageSize"))) {
            pageSize = 5;
        } else {
            pageSize = Integer.parseInt(json.get("pageSize").toString());
        }

        if (!StringUtils.isEmpty(json.get("username"))) {//只传username按username查
            username = json.getString("username");
            if (StringUtils.isEmpty(json.get("condition"))) {//condition=null
                List<AdminPlus> list = adminService.payList(username);
                //   object.put("list",list);
                return getObj(list, pageNum, pageSize);
            }
            condition = json.getString("condition");
            if ("0".equals(condition)) {//全查询
                List<AdminPlus> list = adminService.payList(username);
                //   object.put("list",list);
                return getObj(list, pageNum, pageSize);
            }
            if ("1".equals(condition)) {//username 完成状态为已完成
                List<AdminPlus> list = adminService.queryUserAndComCondition(username);
                // object.put("list",list);
                return getObj(list, pageNum, pageSize);
            }
            if ("2".equals(condition)) {
                List<AdminPlus> list = adminService.queryUserAndUnCondition(username);
                // object.put("list",list);
                return getObj(list, pageNum, pageSize);
            }

        }

        if (StringUtils.isEmpty(json.get("username"))) {
            if (StringUtils.isEmpty(json.get("condition"))) {//condition=null
                List<AdminPlus> list = adminService.payList(username);
                //  object.put("list",list);
                return getObj(list, pageNum, pageSize);
            }
            condition = json.getString("condition");
            if ("0".equals(condition)) {//全查询
                List<AdminPlus> list = adminService.payList(username);
                //  object.put("list",list);
                return getObj(list, pageNum, pageSize);
            }
            if ("1".equals(condition)) {// 完成状态为已完成
                List<AdminPlus> list = adminService.queryCompletePay();
                //   object.put("list", list);
                return getObj(list, pageNum, pageSize);
            }
            if ("2".equals(condition)) {
                List<AdminPlus> list = adminService.queryUndonePay();
                //  object.put("list", list);
                return getObj(list, pageNum, pageSize);
            }
        }
        return object;
    }


    /**
     * 管理员页面查询详情
     */
    @RequestMapping("/queryDetails")
    public JSONObject queryDetails(@RequestBody(required = false) String json1) {
        int pageNum;
        int pageSize;
        if (json1 == null || json1 == "") {
            JSONObject object = new JSONObject();
            object.put("msg", "异常");
            return object;
        }
        JSONObject json = null;
        try {
            json = (JSONObject) JSONObject.parse(json1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (StringUtils.isEmpty(json.get("pageNum"))) {
            pageNum = 1;
        } else {
            pageNum = (int) json.get("pageNum");
        }
        if (StringUtils.isEmpty(json.get("pageSize"))) {
            pageSize = 5;
        } else {
            pageSize = (int) json.get("pageSize");
        }
        String username = "";
        Object obj = request.getSession().getAttribute("username");
        if (obj == null) {
            return null;
        }
        if (request.getSession().getAttribute("username") != null) {
            username = request.getSession().getAttribute("username").toString();
        }
        String merchant_no = "";
        if (StringUtils.isEmpty(json.get("merchant_no"))) {
            merchant_no = "";
        } else {
            merchant_no = json.get("merchant_no").toString();
        }
        String terminalno;
        if (StringUtils.isEmpty(json.get("terminalno"))) {
            terminalno = null;
        } else {
            terminalno = json.getInteger("terminalno").toString();
        }
        String out_trade_no = "";
        if (StringUtils.isEmpty(json.get("out_trade_no"))) {
            out_trade_no = "";
        } else {
            out_trade_no = json.get("out_trade_no").toString();
        }
        PageHelper.startPage(pageNum, pageSize);
        List<Tradeserial> list = adminService.queryDetail(username, merchant_no, terminalno, out_trade_no);
        PageInfo<Tradeserial> pageInfo = new PageInfo<>(list);
        JSONObject object = new JSONObject();
        object.put("pageInfo", pageInfo);
        return object;
    }

    /**
     * 点击详情按钮展示订单列表
     *
     * @param json1
     * @return
     */
    @RequestMapping("/payDetails")
    public JSONObject payDetails(@RequestBody(required = false) String json1) {
        JSONObject object = new JSONObject();
        if (json1 == null || json1 == "") {
            object.put("msg", "缺少参数");
            return object;
        }
        JSONObject json = null;
        try {
            json = (JSONObject) JSONObject.parse(json1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        String username = "";
        if (json.getString("username") != null) {
            username = json.getString("username");
        }
        request.getSession(true).setAttribute("username", username);
        int pageNum;
        int pageSize;
        if (StringUtils.isEmpty(json.get("pageNum"))) {
            List<Tradeserial> list = adminService.payDetails(username);
            object.put("list", list);
            return object;
        } else {
            pageNum = (int) json.get("pageNum");
        }
        if (StringUtils.isEmpty(json.get("pageSize"))) {
            List<Tradeserial> list = adminService.payDetails(username);
            object.put("list", list);
            return object;
        } else {
            pageSize = (int) json.get("pageSize");
        }
        PageHelper.startPage(pageNum, pageSize);
        List<Tradeserial> list = adminService.payDetails(username);
        PageInfo<Tradeserial> pageInfo = new PageInfo<>(list);
        object.put("pageInfo", pageInfo);
        return object;
    }

    public JSONObject getObj(List<AdminPlus> list, int pageNum, int pageSize) {
        JSONObject object = new JSONObject();
        List<AdminPlus> plusList = new ArrayList<>();
        if (list!=null){
            int totalNum = list.size();//总条数
            int totalPage = totalNum % pageSize == 0 ? totalNum / pageSize : totalNum / pageSize + 1;//总页数
      /*  for(int i=(pageNum-1)*pageSize;i<pageSize;i++)
        {
            plusList.add(list.get(i));
        }*/
            if (list != null && list.size() > 0) {
                int currIdx = (pageNum > 1 ? (pageNum - 1) * pageSize : 0);
                for (int i = 0; i < pageSize && i < list.size() - currIdx; i++) {

                    plusList.add(list.get(currIdx + i));
                }
            }
            object.put("list", plusList);
            object.put("totalNum", totalNum);
            object.put("totalPage", totalPage);
            object.put("pageNum", pageNum);
            object.put("pageSize", pageSize);
        }
        return object;
    }
}

